﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace Agile.Service.Rbac
{
    ///<summary>
    ///用户
    ///</summary>
    public interface IUserService : IRepository<User>
    {
        void Switch(string Id);
        User GetByName(string account);
        List<User> GetUsers(string orgId);
    }
    ///<summary>
    ///用户
    ///</summary>
    public partial class UserService : DapperRepository<User>, IUserService
    {
        public User GetByName(string account)
        {
            var user = DBA.GetFirstOrDefault<User>(p => p.Account == account);
            return user;
        }

        public void Switch(string Id)
        {
            var model = GetById(Id);
            if (model != null)
            {
                if (model.Status > 0)
                {
                    model.Status = 0;
                }
                else
                {
                    model.Status = 1;
                }
                Update(model);
            }
        }

        public List<User> GetUsers(string orgId)
        {
            string sql = @"SELECT u.* FROM rbac_user u
INNER JOIN base_relevance r ON u.ID = r.FirstId
INNER JOIN rbac_organize o ON r.SecondId = o.ID WHERE r.[Key]='"+ Define.USERORG + "' AND r.SecondId='"+ orgId + "' ";

            var users= DBA.Query<User>(sql);

            return users.ToList();
        }
    }
}

